/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.actor;

import java.util.ArrayList;
import java.util.List;

import org.openaecfm.model.actor.Address;
import org.openaecfm.model.actor.TelecomAddress;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class TelecomAddressIO extends
		AbstractEntityInstanceInputImpl<TelecomAddress> implements
		IEntityInstanceOutput<TelecomAddress> {

	private IEntityInstanceUtilInput<Address> utilInput;
	private IEntityInstanceUtilOutput<Address> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Address> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(TelecomAddress pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCTELECOMADDRESS",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set TelephoneNumbers
		if (pojo.getTelephoneNumbers() == null
				|| pojo.getTelephoneNumbers().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (String str : pojo.getTelephoneNumbers()) {
				StringParameter s = new StringParameter(str,
						StringParameter.UTF_8);
				l.add(s);
			}
			ei.addParameter(l);
		}

		// Set FacsimileNumbers
		if (pojo.getFacsimileNumbers() == null
				|| pojo.getFacsimileNumbers().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (String str : pojo.getFacsimileNumbers()) {
				StringParameter s = new StringParameter(str,
						StringParameter.UTF_8);
				l.add(s);
			}
			ei.addParameter(l);
		}

		// Set PagerNumber
		if (pojo.getPagerNumber() == null) {
			DollarParameter d = DollarParameter.getInstance();
			ei.addParameter(d);
		} else {
			StringParameter s = new StringParameter(pojo.getPagerNumber(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set ElectronicMailAddresses
		if (pojo.getElectronicMailAddresses() == null
				|| pojo.getElectronicMailAddresses().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (String str : pojo.getElectronicMailAddresses()) {
				StringParameter s = new StringParameter(str,
						StringParameter.UTF_8);
				l.add(s);
			}
			ei.addParameter(l);
		}

		// Set WWWHomePageURL
		if (pojo.getWWWHomePageURL() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			StringParameter s = new StringParameter(pojo.getWWWHomePageURL(),
					StringParameter.UTF_8);
			ei.addParameter(s);
		}

		// Set MessagingIDs
		if (pojo.getMessagingIDs() == null || pojo.getMessagingIDs().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (String str : pojo.getMessagingIDs()) {
				StringParameter s = new StringParameter(str,
						StringParameter.UTF_8);
				l.add(s);
			}
			ei.addParameter(l);
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected TelecomAddress doCreateObject() {
		return new TelecomAddress();
	}

	public void setUtilInput(IEntityInstanceUtilInput<Address> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			TelecomAddress pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set TelephoneNumbers
		Parameter tele = sei.getParameter(3);
		if (tele.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) tele;
			List<String> telephoneNumbers = new ArrayList<String>();
			for (Parameter p : l) {
				StringParameter s = (StringParameter) p;
				telephoneNumbers.add(s.toUTF8());
			}
			pojo.setTelephoneNumbers(telephoneNumbers);
		}

		// Set FacsimileNumbers
		Parameter facs = sei.getParameter(4);
		if (facs.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) facs;
			List<String> facsimileNumbers = new ArrayList<String>();
			for (Parameter p : l) {
				StringParameter s = (StringParameter) p;
				facsimileNumbers.add(s.toUTF8());
			}
			pojo.setFacsimileNumbers(facsimileNumbers);
		}

		// Set PagerNumber
		Parameter page = sei.getParameter(5);
		if (page.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) page;
			pojo.setPagerNumber(s.toUTF8());
		}

		// Set ElectronicMailAddresses
		Parameter email = sei.getParameter(6);
		if (email.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) email;
			List<String> electronicMailAddresses = new ArrayList<String>();
			for (Parameter p : l) {
				StringParameter s = (StringParameter) p;
				electronicMailAddresses.add(s.toUTF8());
			}
			pojo.setElectronicMailAddresses(electronicMailAddresses);
		}

		// Set WWWHomePageURL
		Parameter www = sei.getParameter(7);
		if (www.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) www;
			pojo.setWWWHomePageURL(s.toUTF8());
		}

		// Set MessagingIDs
		Parameter mesIDs = sei.getParameter(8);
		if (mesIDs.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) mesIDs;
			List<String> messagingIDs = new ArrayList<String>();
			for (Parameter p : l) {
				StringParameter s = (StringParameter) p;
				messagingIDs.add(s.toUTF8());
			}
			pojo.setMessagingIDs(messagingIDs);
		}
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			TelecomAddress pojo, IAppDataManager appDataManager) {
		return ;
	}
}
